package org.realogic.btp.sequence.entity;

import java.io.Serializable;

public class Sequence implements Serializable {

	private static final long serialVersionUID = 1886884121778799544L;

	// ID
	private long id;
	// 名称
	private String variableName;
	// 位数
	private int digit;
	// 前缀
	private String prefix;
	// 后缀
	private String suffix;
	// 当前流水值
	private long value;
	// 增量
	private int blockSize;
	// 时间标志
	private int dateflag;

	// 最近修改时间
	private java.sql.Timestamp lastmodtime;

	private long startValue;

	private long endValue;

	/**
	 * Default Constructor
	 */
	public Sequence() {
		// do nothing
	}

	public long getId() {
		return this.id;
	}

	public void setId(long theId) {
		this.id = theId;
	}

	public String getVariableName() {
		return this.variableName;
	}

	public void setVariableName(String theVariableName) {
		this.variableName = theVariableName;
	}

	public int getDigit() {
		return this.digit;
	}

	public void setDigit(int theDigit) {
		this.digit = theDigit;
	}

	public String getPrefix() {
		return this.prefix;
	}

	public void setPrefix(String thePrefix) {
		this.prefix = thePrefix;
	}

	public String getSuffix() {
		return this.suffix;
	}

	public void setSuffix(String theSuffix) {
		this.suffix = theSuffix;
	}

	public long getValue() {
		return this.value;
	}

	public void setValue(long theValue) {
		this.value = theValue;
	}

	public int getBlockSize() {
		return this.blockSize;
	}

	public void setBlockSize(int theBlockSize) {
		this.blockSize = theBlockSize;
	}

	public int getDateflag() {
		return this.dateflag;
	}

	public void setDateflag(int theDateflag) {
		this.dateflag = theDateflag;
	}

	public java.sql.Timestamp getLastmodtime() {
		return this.lastmodtime;
	}

	public void setLastmodtime(java.sql.Timestamp theLastmodtime) {
		this.lastmodtime = theLastmodtime;
	}

	public String toString() {
		StringBuffer returnString = new StringBuffer();
		returnString.append("com.jiuye.pub.sys.entity.SequenceNumber[");
		returnString.append("id = " + this.id + ";\n");
		returnString.append("variableName = " + this.variableName + ";\n");
		returnString.append("digit = " + this.digit + ";\n");
		returnString.append("prefix = " + this.prefix + ";\n");
		returnString.append("suffix = " + this.suffix + ";\n");
		returnString.append("value = " + this.value + ";\n");
		returnString.append("blockSize = " + this.blockSize + ";\n");
		returnString.append("dateflag = " + this.dateflag + ";\n");
		returnString.append("lastmodtime = " + this.lastmodtime + ";\n");
		returnString.append("]\n");
		return returnString.toString();
	}

	public long getStartValue() {
		return startValue;
	}

	public void setStartValue(long startValue) {
		this.startValue = startValue;
	}

	public long getEndValue() {
		return endValue;
	}

	public void setEndValue(long endValue) {
		this.endValue = endValue;
	}

	/**
	 * 获取新的流水号
	 * 
	 * @return String
	 */
	public String newSequenceValue() {
		String value = null;
		if (dateflag > 0) {
			value = prefix.concat(String.valueOf(dateflag)).concat(fix(startValue, getDigit()));
		} else {
			value = prefix.concat(fix(startValue, getDigit()));
		}
		return value;
	}

	public String newSequenceValue(long current) {
		if (current < startValue || current > endValue) {
			throw new IllegalStateException(current + "值必须在[" + startValue + "~" + endValue + "]之间...");
		}
		String value = null;
		if (dateflag > 0) {
			value = prefix.concat(String.valueOf(dateflag)).concat(fix(current, getDigit()));
		} else {
			value = prefix.concat(fix(current, getDigit()));
		}
		return value;
	}

	private String fix(long theValue, int theLength) {
		String s = String.valueOf(theValue);
		if (dateflag > 0) {
			if (theLength <= 0) {
				theLength = 3;
			}
		}
		if (theLength > 0) {
			int y = theLength - s.length();
			s = zeroRepeat(y).concat(s);
		}
		return s;
	}

	private String zeroRepeat(int len) {
		String rtn = null;
		switch (len) {
		case 1:
			rtn = "0";
			break;
		case 2:
			rtn = "00";
			break;
		case 3:
			rtn = "000";
			break;
		case 4:
			rtn = "0000";
			break;
		case 5:
			rtn = "00000";
			break;
		case 6:
			rtn = "000000";
			break;
		case 7:
			rtn = "0000000";
			break;
		case 8:
			rtn = "00000000";
			break;
		case 9:
			rtn = "000000000";
			break;
		case 10:
			rtn = "0000000000";
			break;
		case 11:
			rtn = "00000000000";
			break;
		case 12:
			rtn = "000000000000";
		}
		return rtn;
	}

}
